package de.hhbk.feedbackmodul.daobeans;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import de.hhbk.feedbackmodul.daointerfaces.AnswerPossibilityDaoIF;
import de.hhbk.feedbackmodul.entities.AnswerPossibility;

@Stateless
public class AnswerPossibilityDaoBean implements AnswerPossibilityDaoIF {

	@PersistenceContext(unitName = "FeedbackModulEJB")
	private EntityManager manager;
	
	private Logger logger = Logger.getLogger(this.getClass());
	

	@SuppressWarnings("unchecked")
	@Override
	public List<AnswerPossibility> getAllAnswerPossibilities() throws Exception  {
		try
		{
			Query query = manager.createQuery("select ap from AnswerPossibility ap", AnswerPossibility.class);
			return (List<AnswerPossibility>) query.getResultList();
		}
		catch(Exception e)
		{
			logger.error("Error while reading AnswerPossibilities: "+e.getMessage(), e);
			throw e;
		}
	}


	@Override
	public void createAnswerPossibility(AnswerPossibility answerPossibility) throws Exception {
		try
		{
			manager.persist(answerPossibility);
		}
		catch(Exception e)
		{
			logger.error("Error while persisting AnswerPossibility: "+e.getMessage(), e);
			throw e;
		}
	}


	@Override
	public void updateAnswerPossibility(AnswerPossibility answerPossibility) throws Exception {
		try
		{
			manager.merge(answerPossibility);
		}
		catch(Exception e)
		{
			logger.error("Error while updating AnswerPossibility: "+e.getMessage(), e);
			throw e;
		}
	}


	@Override
	public void deleteAnswerPossibility(AnswerPossibility answerPossibility)
			throws Exception {
		try
		{
			AnswerPossibility answerPossibilityToDelete = manager.find(AnswerPossibility.class, answerPossibility.getId());
			manager.remove(answerPossibilityToDelete);
		}
		catch(Exception e)
		{
			logger.error("Error while deleting AnswerPossibility: "+e.getMessage(), e);
			throw e;
		}
		
	}

}
